﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class StaffClass
    {
        public int Add(Chain.Model.StaffClass model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into StaffClass(");
            builder.Append("ClassName,ClassType,ClassPercent,ClassShopID,ClassRemark)");
            builder.Append(" values (");
            builder.Append("@ClassName,@ClassType,@ClassPercent,@ClassShopID,@ClassRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassName", SqlDbType.NVarChar, 50), new SqlParameter("@ClassType", SqlDbType.Bit, 1), new SqlParameter("@ClassPercent", SqlDbType.Float, 8), new SqlParameter("@ClassShopID", SqlDbType.Int, 4), new SqlParameter("@ClassRemark", SqlDbType.NVarChar, 500) };
            cmdParms[0].Value = model.ClassName;
            cmdParms[1].Value = model.ClassType;
            cmdParms[2].Value = model.ClassPercent;
            cmdParms[3].Value = model.ClassShopID;
            cmdParms[4].Value = model.ClassRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Delete(int ClassID)
        {
            DataTable table = DbHelperSQL.Query("select StaffID,staffClassID from Staff where staffClassID=" + ClassID).Tables[0];
            if (table.Rows.Count > 0)
            {
                return -1;
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from StaffClass ");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }

        public bool DeleteList(string ClassIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from StaffClass ");
            builder.Append(" where ClassID in (" + ClassIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from StaffClass");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int intClassID, string strClassName, int ShopID)
        {
            string format = "select ClassName from StaffClass where ClassID not in ({0}) and (ClassName = '{1}') and ClassShopID = '{2}'";
            DataTable table = DbHelperSQL.Query(string.Format(format, intClassID, strClassName, ShopID)).Tables[0];
            return (table.Rows.Count > 0);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ClassID,ClassName,ClassType,ClassPercent,ClassShopID,ShopName,ClassRemark ");
            builder.Append(" FROM StaffClass,SysShop ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ClassID,ClassName,ClassType,ClassPercent,ClassShopID,ClassRemark ");
            builder.Append(" FROM StaffClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ClassID desc");
            }
            builder.Append(")AS Row, T.*  from StaffClass T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = " StaffClass,SysShop ";
            string[] column = new string[] { " * " };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ClassShopID", "ClassID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ClassID", "StaffClass");
        }

        public Chain.Model.StaffClass GetModel(int ClassID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ClassID,ClassName,ClassType,ClassPercent,ClassShopID,ClassRemark from StaffClass ");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ClassID;
            Chain.Model.StaffClass class2 = new Chain.Model.StaffClass();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ClassID"] != null) && (set.Tables[0].Rows[0]["ClassID"].ToString() != ""))
            {
                class2.ClassID = int.Parse(set.Tables[0].Rows[0]["ClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ClassName"] != null) && (set.Tables[0].Rows[0]["ClassName"].ToString() != ""))
            {
                class2.ClassName = set.Tables[0].Rows[0]["ClassName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ClassType"] != null) && (set.Tables[0].Rows[0]["ClassType"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["ClassType"].ToString() == "1") || (set.Tables[0].Rows[0]["ClassType"].ToString().ToLower() == "true"))
                {
                    class2.ClassType = true;
                }
                else
                {
                    class2.ClassType = false;
                }
            }
            if ((set.Tables[0].Rows[0]["ClassPercent"] != null) && (set.Tables[0].Rows[0]["ClassPercent"].ToString() != ""))
            {
                class2.ClassPercent = decimal.Parse(set.Tables[0].Rows[0]["ClassPercent"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ClassShopID"] != null) && (set.Tables[0].Rows[0]["ClassShopID"].ToString() != ""))
            {
                class2.ClassShopID = int.Parse(set.Tables[0].Rows[0]["ClassShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ClassRemark"] != null) && (set.Tables[0].Rows[0]["ClassRemark"].ToString() != ""))
            {
                class2.ClassRemark = set.Tables[0].Rows[0]["ClassRemark"].ToString();
            }
            return class2;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM StaffClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.StaffClass model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update StaffClass set ");
            builder.Append("ClassName=@ClassName,");
            builder.Append("ClassType=@ClassType,");
            builder.Append("ClassPercent=@ClassPercent,");
            builder.Append("ClassShopID=@ClassShopID,");
            builder.Append("ClassRemark=@ClassRemark");
            builder.Append(" where ClassID=@ClassID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ClassName", SqlDbType.NVarChar, 50), new SqlParameter("@ClassType", SqlDbType.Bit, 1), new SqlParameter("@ClassPercent", SqlDbType.Float, 8), new SqlParameter("@ClassShopID", SqlDbType.Int, 4), new SqlParameter("@ClassRemark", SqlDbType.NVarChar, 500), new SqlParameter("@ClassID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ClassName;
            cmdParms[1].Value = model.ClassType;
            cmdParms[2].Value = model.ClassPercent;
            cmdParms[3].Value = model.ClassShopID;
            cmdParms[4].Value = model.ClassRemark;
            cmdParms[5].Value = model.ClassID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

